/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.cancel;

import cn.hutool.core.util.StrUtil;
import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.CancelOrderSuccessEvent;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.OmsOrder;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.lxk.oms.flow.cancel.FlowCancelOrderNodeConst.CANCEL_ORDER_NODE;

/**
 * 取消订单
 *
 * @author heyi
 * 2024/9/3
 */
@AllArgsConstructor
@LiteflowComponent(CANCEL_ORDER_NODE)
public class OrderCancelNode extends NodeComponent {
    private final IOmsOrderService omsOrderService;

    @Override
    public void process() throws Exception {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
        Long orderId = omsOrderVo.getId();
        OmsOrder update = new OmsOrder();
        update.setCancelTime(new Date());
        update.setCancelReason(contextBean.getCancelReason());
        update.setActReceiveAmount(BigDecimal.ZERO);
        update.setRefundAmount(omsOrderVo.getActPayPrice());
        List<OrderEnums.OrderStatus> srcCancelStatusList = contextBean.getSrcCancelStatusList();
        Boolean success = omsOrderService.updateBatchOrderStatusWithInfo(orderId,
            srcCancelStatusList
            , contextBean.getCancelStatus(), update);
        if (!success) {
            contextBean.setMsg(StrUtil.format("当前订单状态:{}, 无法取消", OrderEnums.OrderStatus.getByCode(omsOrderVo.getStatus()).getName()));
        }
        contextBean.setSuccess(success);
    }

    @Override
    public void onSuccess() throws Exception {
        CancelOrderContext contextBean = this.getContextBean(CancelOrderContext.class);
        if (contextBean.getSuccess()) {
            // 发送操作事件
            OrderOptionLogEvent optionLogEvent = new OrderOptionLogEvent();
            OmsOrderVo omsOrderVo = contextBean.getOmsOrderVo();
            optionLogEvent.setOrderSn(omsOrderVo.getOrderNo());
            optionLogEvent.setUserId(contextBean.getUserId());
            optionLogEvent.setOptionUserId(contextBean.getOptionUserId());
            optionLogEvent.setOptionUserType(contextBean.getOptionUserType());
            optionLogEvent.setContent(OrderOptionLogEvent.OrderContentFormatEnums.CANCEL_ORDER.formatContent(contextBean.getCancelStatus().getName(),
                StrUtil.blankToDefault(contextBean.getCancelReason(), "-")
            ));
            optionLogEvent.setUserType(contextBean.getUserType());
            optionLogEvent.setMerId(omsOrderVo.getMerId());
            optionLogEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
            optionLogEvent.setTenantId(contextBean.getTenantId());
            EventHelper.publishEvent(optionLogEvent);
            // 发送成功事件
            CancelOrderSuccessEvent event = new CancelOrderSuccessEvent();
            event.setOrderNo(omsOrderVo.getOrderNo());
            event.setActPayPrice(omsOrderVo.getActPayPrice());
            event.setCancelCode(contextBean.getCancelStatus().getCode());
            event.setUserId(omsOrderVo.getUserId());
            event.setTenantId(contextBean.getTenantId());
            event.setOrderType(omsOrderVo.getOrderType());
            event.setOrderId(omsOrderVo.getId());
            event.setOrderName(omsOrderVo.getOrderName());
            event.setOrderStatus(OrderEnums.OrderStatus.getByCode(omsOrderVo.getStatus()));
            EventHelper.publishEvent(event);
        }
    }
}
